<!--拌和站---拌合站设置-->
<!--
 * @Author: 卡卡 1071780426@qq.com
 * @Date: 2022-12-06 13:13:25
 * @LastEditors: CNN 969682281@qq.com
 * @LastEditTime: 2023-06-30 14:44:32
 * @FilePath: \backstage - 副本\src\views\aa.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="department" id="department">
    <!-- 左侧部门管理 -->
    <div class="structureTree" v-if="treelength">
      <div class="structureTree_title">
        <div>组织机构</div>
        <div></div>
      </div>
      <tree
        :treeData="treeData"
        :checkData="checkData"
        nodeKey="fCompanyid"
        @getTreeInfo="handleNodeClick"
        :defaultProps="defaultProps"
      >
      </tree>
    </div>
    <!-- 右侧 -->
    <div class="department_right">
      <!-- 机构名称 -->
      <el-breadcrumb :title="showName"></el-breadcrumb>
      <!-- 顶部导航栏 -->
      <div class="mechanism_nav">
        <!-- 左侧 -->
        <div class="mechanism_nav_left"></div>
        <!-- 顶部按钮 -->
        <div class="print" style="justify-content: flex-end">
          <el-button
            type="primary"
            icon="el-icon-refresh"
            size="small"
            @click="yj_searchForm(getList)"
          >
          </el-button>

          <el-button
            type="primary"
            v-if="yj_view"
            icon="el-icon-search"
            size="small"
            @click="yj_editForm(2)"
            >查看
          </el-button>
          <el-button
            type="primary"
            v-if="yj_add"
            icon="el-icon-circle-plus-outline"
            size="small"
            @click="yj_editForm(0)"
          >
            新增</el-button
          >
          <el-button
            type="primary"
            v-if="yj_edit"
            icon="el-icon-edit"
            size="small"
            @click="yj_editForm(1)"
            >编辑
          </el-button>
        </div>
      </div>
      <!-- 表格 -->
      <div class="text_table" id="sqmixingPlant">
        <tableData
          :config="table_config"
          :rowKey="rowKey"
          :dataList="tableData"
          :loading="listLoading"
          @selection-change="selectionChange"
          @sort-change="sortChange"
          @click-change="rowChange"
          @openDialog="openDialog"
        />
      </div>
      <!--弹框 查看 -->
      <div class="Control-1 bansetting">
        <el-dialog
          :title="editDialog"
          :modal-append-to-body="false"
          v-dialogDrag
          :visible.sync="dialogEditVisible"
          v-loading="loadingEditDialog"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
        >
          <div class="dialogBody common" :style="{ height: '550px' }">
            <div class="baseTitle">基本信息</div>
            <div style="height: 160px">
              <publicForm
                ref="publicForm"
                lableWidth="60px"
                @changeRadio="changeRadio"
                :formObj="formObj"
                :formData="formData"
                @selectChange="selectChange"
              >
              </publicForm>
            </div>
            <div class="baseTitle" v-if="formData.fType != 0">骨料详情</div>
            <div
              style="height: 75px; margin-top: 8px"
              class="editTableAlarm editTableAlarms"
              v-if="formData.fType != 0"
            >
              <tableData
                :config="material_config"
                :dataList="materialData"
                :currentRow="false"
                @inputValue="inputValue"
              />
            </div>
          </div>
          <div class="newlyAdded_btn" v-if="submitFlag">
            <el-button type="primary" @click="yj_saveForm('formData')"
              >保存</el-button
            >
            <el-button type="warning" @click="dialogEditVisible = false"
              >关闭</el-button
            >
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
import tree from "@/components/public/el-trees"; //左侧机构树
import TableData from "@/components/public/mixingPlant_table";
import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import publicForm from "@/components/public/el-form.vue";
import {
  getUserCompanyTreeList, //树状图接口
  getStationmachineList, //列表
  saveStationmachine, //新增
  getStationmachineByFid, //编辑详情
} from "@/api/request.js";
import { type } from "@/utils/config";
export default {
  name: "Home",
  data() {
    return {
      name: "mixingPlant", //按钮配置页面代号
      yj_add: null, //新增按钮是否显示
      yj_edit: null, //编辑按钮是否显示
      yj_view: null, //查看按钮是否显示
      submitFlag: null, //是否显示保存按钮
      formObj: [
        {
          //新增编辑配置参数
          radio: true,
          label: "类型",
          prop: "fType",
          placeholder: "请选择",
          width: 24, //参考el-col
          disabled: false,
          options: [
            {
              label: "拌合站",
              value: "0",
            },
            {
              label: "拌合机",
              value: "1",
            },
          ],
        },
        {
          select: true,
          label: "拌合站",
          prop: "fStationname",
          placeholder: "请选择",
          width: 12, //参考el-col
          disabled: false,
          options: [],
          rules: [
            {
              required: true,
              message: "请选择拌合站",
            },
          ], //验证
        },
        {
          input: true, //是input
          label: "远程号码",
          prop: "fRemotenumber",
          width: 12, //参考el-col
          disabled: false,
          rules: [
            {
              required: true,
              message: "远程不能为空",
            },
          ], //验证
        },
        //
        {
          input: true, //是input
          label: "编号", //字段
          prop: "fMachineid", //字段名
          width: 12, //参考el-col
          disabled: false, //是否禁用
          rules: [
            {
              required: true,
              message: "编号不能为空",
            },
          ], //验证
        },

        {
          input: true, //是input
          label: "名称", //字段
          prop: "fMachinename", //字段名
          width: 12, //参考el-col
          disabled: false, //是否禁用
          rules: [
            {
              required: true,
              message: "编号不能为空",
            },
          ], //验证
        },

        {
          textarea: true, //是input
          label: "数据库表名", //字段
          prop: "fNote", //字段名
          width: 24, //参考el-col
          disabled: false, //是否禁用
        },
      ],
      formData: {}, //新增编辑向后台传递对象
      editDialog: null, //dialog的title新增、编辑、查看
      glInfo: null, //骨料详情对象，页面编辑发生改变
      rowKey: "fId", //拌和站树状列表rowKey
      // 拌和站列表配置项
      table_config: {
        imgKeyWord: "url",
        fileKeyWord: "url",
        fileName: "fFileName",
        dialogHeight: "",
        thead: [
          {
            label: "施工单位",
            prop: "fCompanyname",
            minWidth: "150px",
            routerPath: "xxx",
            align: "left",
            display: "bhz_flex",
          },
          {
            label: "编号",
            prop: "fMachineid",
            minWidth: "100px",
            routerPath: "xxx",
            align: "center",
            display: "inline",
          },
          {
            label: "远程号码",
            prop: "fRemotenumber",
            minWidth: "150px",
            routerPath: "xxx",
            align: "center",
            display: "inline",
          },
          //
          {
            label: "名称",
            prop: "fMachinename",
            minWidth: "100px",
            routerPath: "xxx",
            align: "center",
            display: "inline",
          },

          {
            label: "类型",
            prop: "fType",
            minWidth: "150px",
            filter: true,
            align: "center",
            btn: true,
            colorName: "",
            display: "inline",
            callback: (rowData) => {
              let data = type[rowData.fType];
              if (data) {
                return data.label;
              }
            },
            specialClass: (rowData) => {
              let data = type[rowData.fType];
              if (data) {
                return data.className;
              }
            },
          },
          {
            label: "备注",
            prop: "fNote",
            minWidth: "100px",
            routerPath: "xxx",
            display: "inline",
          },
        ],
        isCheckBox: false,
        isIndex: true,
        isOperation: true,
        // 表格操作列
        // operation: {
        //     label: '操作',
        //     width: '100',
        //     detail: true,
        //     cols: [
        //         {
        //             label: '查看',
        //             type: 'primary',
        //             handleRow: this.handleRow
        //         },

        //     ]
        // },
      },
      // 骨料表格配置参数
      material_config: {
        thead: [
          {
            label: "原名",
            prop: "name",
            minWidth: "70px",
            disabled: false, //是否禁用
            edit: false,
            type: "text",
            align: "center",
          },
          {
            label: "骨料1",
            prop: "fParm1",
            minWidth: "70px",
            disabled: false, //是否禁用
            edit: true,
            type: "text",
            align: "center",
          },
          {
            label: "骨料2",
            prop: "fParm2",
            minWidth: "70px",
            disabled: false, //是否禁用
            edit: true,
            type: "text",
            align: "center",
          },
          {
            type: "text",
            label: "骨料3",
            prop: "fParm3",
            minWidth: "70px",
            disabled: false,
            edit: true, //是否禁用
            align: "center",
          },
          {
            type: "text",
            label: "骨料4",
            prop: "fParm4",
            minWidth: "70px",
            disabled: false,
            edit: true, //是否禁用
            align: "center",
          },
          {
            type: "text",
            label: "骨料5",
            prop: "fParm5",
            minWidth: "70px",
            disabled: false,
            edit: true, //是否禁用
            align: "center",
          },
          {
            type: "text",
            label: "骨料6",
            prop: "fParm6",
            minWidth: "70px",
            disabled: false,
            edit: true, //是否禁用
            align: "center",
          },
        ],
      }, //材料配置项
      materialData: [
        {
          name: "现名",
          fParm1: "骨料1", //骨料1
          fParm2: "骨料2", //骨料2
          fParm3: "骨料3", //骨料3
          fParm4: "骨料4", //骨料4
          fParm5: "骨料5", //骨料5
          fParm6: "骨料6", //骨料6
        },
      ], //材料用量列表

      highlightBd: true, //左侧机构树停留背景色
      treelength: false, //左侧树显示隐藏
      checked: true, //查看下级
      showName: "未选择公司",
      dialogHeight: "330px", //新增编辑弹框高度
      fId: null, //点击当前列表数据fId
      listLoading: false, //列表数据加载loading
      loadingEditDialog: false, //弹框loading
      dialogEditVisible: false, //新增弹框是否显示
      // 列表给后端传参
      getList: {
        companyId: "", // 施工标段id
        modelType: "", // 类型
        stationType: "1",
      },

      companyTree: "", //所有数据
      treeData: [], //左侧树状图数据
      tableData: [], //表格列表数据列表
      // 左侧机构树
      defaultProps: {
        children: "children",
        label: "fShortname",
      },
      userInfo: {}, //登录用户
      checkData: [],
      treeText: {
        //树状图参数
        companyId: null,
        userId: null,
        text: null,
      },
    };
  },
  components: {
    tree, //左侧组织机构数
    TableData, //主页面列表
    elBreadcrumb, //面包屑
    publicForm, //弹框表格
  },
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
    if (JSON.parse(sessionStorage.getItem("yjSmart-treeCheck"))) {
      var id = JSON.parse(sessionStorage.getItem("yjSmart-treeCheck"));
      this.checkData = [id.fCompanyid];
    }

    this.getTree();
    this.btnRole(this.name);
  },
  methods: {
    btnRole(name) {
      // 按钮权限
      var btnList = [];
      var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
      if (ModuleList.length > 0) {
        ModuleList.map((item) => {
          if (item.fEncode == name) {
            btnList = item.modulebuttonList;
          }
        });
        var btnStr = [];
        btnList.map((item) => {
          btnStr.push(item.fEncode);
        });

        this.yj_add = btnStr.includes("yj_add");
        this.yj_edit = btnStr.includes("yj_edit");
        this.yj_view = btnStr.includes("yj_view");
      } else {
        this.yj_add = true;
        this.yj_edit = true;
        this.yj_view = true;
      }
    },
    // 点击列表当前行，获取当前信息
    rowChange(row) {
      this.fId = row.fId;
    },
    // 左侧树结构
    getTree() {
      this.treeText = {
        companyId: this.userInfo.companyId,
        userId: this.userInfo.userId,
        text: "0", // 0其他  1 砼拌合站 2水稳 3沥青
      };
      getUserCompanyTreeList(this.treeText)
        .then((res) => {
          if (res.errcode == 0) {
            var companyTree = res.data.userCompanyTreeList;
            this.treeData = this.commonJs.tranListToTreeData(companyTree);

            if (this.treeData[0].children == undefined) {
              this.treelength = false; //左侧组织机构是否显示
            } else {
              this.treelength = true;
            }
            if (this.checkData[0]) {
              this.showName = JSON.parse(
                sessionStorage.getItem("yjSmart-treeCheck")
              ).fShortname;
              this.getList.companyId = this.checkData[0];
            } else {
              this.checkData[0] = this.treeData[0].fCompanyid;
              this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
              this.getList.companyId = this.treeData[0].fCompanyid; //当前点击组织机构ID
            }
            // this.showName = this.treeData[0].fShortname;//当前点击组织机构名称
            // this.getList.companyId = this.treeData[0].fCompanyid;//当前点击组织机构ID
            this.yj_searchForm(this.getList); // 调用主页面列表接口
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch(() => {
          return false;
        });
    },

    // 点击左侧机构树
    handleNodeClick(data) {
      this.fId = "";
      this.showName = data.fShortname; //顶部显示机构名
      this.getList.companyId = data.fCompanyid;
      this.yj_searchForm(this.getList);
    },
    tranListToTreeDatas(data) {
      //处理主页面列表树结构
      let result = [];
      if (!Array.isArray(data)) {
        return result;
      }
      // 清除已存在的数据  重新赋值
      data.forEach((item) => {
        delete item.children;
      });
      let map = {};
      data.forEach((item) => {
        map[item.fMachineid] = item;
      });
      data.forEach((item) => {
        let parent = map[item.fStationid];
        if (parent) {
          (parent.children || (parent.children = [])).push(item);
        } else {
          result.push(item);
        }
      });
      return result;
    },
    // 获取列表数据
    yj_searchForm(data) {
      this.listLoading = true;
      getStationmachineList(data)
        .then((res) => {
          if (res.errcode == 0) {
            var that = this;
            that.listLoading = false;
            that.tableData = that.tranListToTreeDatas(
              res.data.stationmachineList
            );
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 新增、编辑
    yj_editForm(status) {
      //0新增，1编辑
      //
      this.mixingPlantList(); //点击新增编辑获取拌合机数据
      if (status == 1) {
        this.submitFlag = true; //显示保存按钮
        this.editDialog = "编辑功能";
        if (this.fId != null && this.fId != "") {
          this.dialogEditVisible = true;
          getStationmachineByFid({ fId: this.fId })
            .then((res) => {
              if (res.errcode == 0) {
                // 判断页面input select 必填项是否禁用等
                this.formObj.map((item) => {
                  item.disabled = false;
                  if (item.rules) {
                    item.rules[0].required = true;
                  }
                });
                this.material_config.thead.map((item) => {
                  item.disabled = false; //是否禁用
                });

                if (res.data.yjQmpMsStationmachine.fType == 0) {
                  this.formObj[1].disabled = true;
                  this.formObj[1].rules[0].required = false;
                  this.formObj[2].disabled = true;
                  this.formObj[2].rules[0].required = false;
                  this.formObj[5].label = "备注";
                } else {
                  this.formObj[1].disabled = false;
                  this.formObj[1].rules[0].required = true;
                  this.formObj[2].disabled = false;
                  this.formObj[2].rules[0].required = true;
                  this.formObj[5].label = "数据库表名";
                }
                // 判断页面input select 必填项是否禁用等
                this.loadingEditDialog = false;
                this.formData = res.data.yjQmpMsStationmachine;
                this.materialData = [
                  {
                    name: "现名",
                    fParm1: this.formData.fParm1, //骨料1
                    fParm2: this.formData.fParm2, //骨料2
                    fParm3: this.formData.fParm3, //骨料3
                    fParm4: this.formData.fParm4, //骨料4
                    fParm5: this.formData.fParm5, //骨料5
                    fParm6: this.formData.fParm6, //骨料6
                  },
                ];
              } else {
                this.$message.error(res.msg);
                console.log("接口报错");
              }
            })
            .catch((error) => {
              console.log("拌合站设置编辑报错", error);
            });
        } else {
          return this.$message.error("请选择需要编辑的数据");
        }
      } else if (status == 0) {
        this.submitFlag = true; //显示保存按钮
        if (this.getList.companyId != "" && this.getList.companyId != null) {
          this.editDialog = "新增";
          this.dialogEditVisible = true;
          this.loadingEditDialog = false;
          // 判断页面input select 必填项是否禁用等
          this.formObj.map((item) => {
            item.disabled = false;
            if (item.rules) {
              item.rules[0].required = true;
            }
          });
          this.material_config.thead.map((item) => {
            item.disabled = false; //是否禁用
          });
          // 判断页面input select 必填项是否禁用等
          this.formData = {
            fId: null, // 主键id
            fCompanyid: this.getList.companyId, // 施工单位id
            fCompanyname: this.showName, //施工单位
            fMachineid: "", // 编号
            fMachinename: "", // 名称
            fStationid: "", // 拌合站id
            fStationname: "", // 拌合站名称
            fRemotenumber: "",
            fStationtype: "1",
            fType: "1", // 类型  0拌合站 1拌合机
            fNote: "", //备注
            fParm1: "骨料1", //骨料1
            fParm2: "骨料2", //骨料2
            fParm3: "骨料3", //骨料3
            fParm4: "骨料4", //骨料4
            fParm5: "骨料5", //骨料5
            fParm6: "骨料6", //骨料6
          };
        } else {
          return this.$message.error("请先选择施工单位");
        }
      } else if (status == 2) {
        this.editDialog = "查看功能";
        this.submitFlag = false; //不显示提交按钮

        if (this.fId != null && this.fId != "") {
          this.dialogEditVisible = true;

          getStationmachineByFid({ fId: this.fId })
            .then((res) => {
              if (res.errcode == 0) {
                // 判断页面input select 必填项是否禁用等
                this.formObj.map((item) => {
                  item.disabled = true;
                  if (item.rules) {
                    item.rules[0].required = false;
                  }
                });
                this.material_config.thead.map((item) => {
                  item.disabled = true; //是否禁用
                });
                // 判断页面input select 必填项是否禁用等
                this.loadingEditDialog = false;
                this.formData = res.data.yjQmpMsStationmachine;
                this.materialData = [
                  {
                    name: "现名",
                    fParm1: this.formData.fParm1, //骨料1
                    fParm2: this.formData.fParm2, //骨料2
                    fParm3: this.formData.fParm3, //骨料3
                    fParm4: this.formData.fParm4, //骨料4
                    fParm5: this.formData.fParm5, //骨料5
                    fParm6: this.formData.fParm6, //骨料6
                  },
                ];

                if (res.data.yjQmpMsStationmachine.fType == 0) {
                  this.formObj[5].label = "备注";
                } else {
                  this.formObj[5].label = "数据库表名";
                }
              } else {
                this.$message.error(res.msg);
                console.log("接口报错");
              }
            })
            .catch((error) => {
              console.log("拌合站设置编辑报错", error);
            });
        } else {
          return this.$message.error("请选择需要查看的数据");
        }
      }
    },
    // 保存接口
    yj_saveForm() {
      if (this.$refs.publicForm.submitForm()) {
        this.formData.fStationtype = "1";
        var newObj = {};
        // delete this.glInfo.name
        Object.assign(newObj, this.formData, this.glInfo);

        saveStationmachine(newObj)
          .then((res) => {
            if (res.errcode == 0) {
              this.yj_searchForm(this.getList);
              this.dialogEditVisible = false;
              this.$message.success("新增成功");
            } else {
              this.$message.error(res.msg);
              console.log("接口报错");
            }
          })
          .catch((error) => {
            console.log("拌合站设置-新增）", error);
          });
      } else {
        console.log("error");
      }
    },
    // 删除
    yj_delForm(id) {
      console.log(id);
    },
    // 获取拌合站数据
    mixingPlantList() {
      var data = {
        companyId: this.getList.companyId, // 施工标段id
        modelType: "0", // 类型
        stationType: "1",
      };
      getStationmachineList(data)
        .then((res) => {
          if (res.errcode == 0) {
            var arr = res.data.stationmachineList;
            this.formObj[1].options = [];
            arr.map((item) => {
              this.formObj[1].options.push({
                label: item.fMachinename,
                value: item.fMachineid,
              });
            });
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 关闭新增编辑弹框
    resetForm() {
      this.dialogEditVisible = false;
    },
    // 类型改变时触发
    changeRadio(num) {
      if (num == 0) {
        this.formObj[1].disabled = true;
        this.formObj[1].rules[0].required = false;
        this.formObj[2].rules[0].required = false;
        this.formObj[2].disabled = true;

        this.formData.fStationid = ""; // 拌合站id
        this.formData.fStationname = ""; // 拌合站名称

        this.formData.fRemotenumber = ""; //远程号码
        this.formObj[5].label = "备注";
      } else {
        this.formObj[1].disabled = false;
        this.formObj[1].rules[0].required = true;

        this.formObj[2].rules[0].required = true;
        this.formObj[2].disabled = false;
        this.formObj[5].label = "数据库表名";
      }
    },
    // 拌合站下拉框
    selectChange(value) {
      this.formData.fStationid = value.value; // 拌合站id
      this.formData.fStationname = value.label; // 拌合站名称
    },
    // 骨料详情
    inputValue(val) {
      this.glInfo = val;
    },
    // 多选方法
    selectionChange() {},
    // 排序方法
    sortChange() {},
    // 点击设备编码
    openDialog(row) {
      this.fId = row.F_Id;
      this.editText(this.fId);
    },
  },
};
</script>
  
<style scoped>
.printTables {
  background: #fff;
  display: flex;
  justify-content: end;
  padding: 5px;
  margin-top: 10px;
}

.dialogBody {
  padding: 3px 20px;
  height: 80%;
  overflow-y: auto;
}

.baseTitle {
  /* border: 1px solid #0c204d; */
  border-bottom: 1px solid #0c204d;

  height: 30px;
  line-height: 30px;
  position: relative;
  padding-left: 15px;
}

.baseTitle::after {
  display: block;
  content: "";
  height: 30px;
  width: 5px;
  position: absolute;
  left: 0;
  top: 0;
  background: #0c204d;
}

#sqmixingPlant /deep/.el-table tr td:nth-child(2) .cell {
  display: flex !important;
}

#sqmixingPlant /deep/.el-table .el-table__expand-icon {
  height: 30px;
  line-height: 30px;
}
</style>